#!/usr/bin/env python
# encoding: utf-8
"""
@author: youfeng
@email: youfeng243@163.com
@license: Apache Licence
@file: sync_new_regist_company.py
@time: 2018/1/22 17:16
"""

import sys
from datetime import datetime

sys.path.append('..')
sys.path.append('../..')
from common import util
from common.mongo import MongDb
from logger import Logger

APP_DATA_MONGO_CONF = {
    'host': '172.16.215.16',
    'port': 40042,
    'db': 'app_data',
    'username': 'work',
    'password': 'haizhi'
}

COMPANY_DATA_MONGO_CONF = {
    'host': '172.16.215.2',
    'port': 40042,
    'db': 'company_data',
    'username': 'work',
    'password': 'haizhi'
}

# 日志模块
log = Logger('sync_new_regist_company.log').get_logger()

app_data_db = MongDb(APP_DATA_MONGO_CONF['host'], APP_DATA_MONGO_CONF['port'], APP_DATA_MONGO_CONF['db'],
                     APP_DATA_MONGO_CONF['username'],
                     APP_DATA_MONGO_CONF['password'], log=log)

company_data_db = MongDb(COMPANY_DATA_MONGO_CONF['host'], COMPANY_DATA_MONGO_CONF['port'],
                         COMPANY_DATA_MONGO_CONF['db'],
                         COMPANY_DATA_MONGO_CONF['username'],
                         COMPANY_DATA_MONGO_CONF['password'], log=log)


def get_change_date(origin_date):
    time_obj = datetime.strptime(origin_date, '%Y年%m月%d日')

    return time_obj.strftime('%Y-%m-%d')


def main():
    table_2018_name = 'zhejiang_ningbo_2017'
    source_table_name = 'new_zhejiang_search_list'

    result_2018_list = []
    total = 0
    count_2018 = 0
    for item in company_data_db.traverse_batch(source_table_name, {'register_date': {'$gte': '2018'}}):
        total += 1
        register_date_src = item.get('register_date')
        if not isinstance(register_date_src, basestring):
            continue

        company = item.get('_id')

        # 格式化时间信息
        try:
            register_date = get_change_date(register_date_src)
        except Exception as e:
            log.info("日期格式转换失败: company = {}".format(company))
            log.exception(e)
            continue

        register_code = item.get('register_code')
        status = item.get('status')

        data_2018 = {
            '_id': company,
            '_in_time': util.get_now_time(),
            'status': status,
            'register_code': register_code,
            'register_date': register_date,
        }

        if u'2018' in register_date_src or '2018' in register_date_src:
            result_2018_list.append(data_2018)
            count_2018 += 1
            if len(result_2018_list) >= 500:
                app_data_db.insert_batch_data(table_2018_name, result_2018_list)
                del result_2018_list[:]

        if total % 1000 == 0:
            log.info("当前进度: total = {} 2018 = {}".format(total, count_2018))

    if len(result_2018_list) > 0:
        app_data_db.insert_batch_data(table_2018_name, result_2018_list)
        del result_2018_list[:]

    log.info("同步完成...")


if __name__ == '__main__':
    main()
